package com.zhiyou.crud.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.crud.bean.Employee;
import com.zhiyou.crud.bean.Msg;
import com.zhiyou.crud.service.EmployeeService;
import com.zhiyou.crud.service.impl.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * @author leibusi
 * @desc
 * @create 2021-10-16 14:43
 */
@Controller
public class EmployController {

  @Autowired
  EmployeeService employeeService;

    @RequestMapping("/emps")
   /* public String getEmps(){
        System.out.println("-----------------");
        List<Employee> list = employeeService.getAll();
        System.out.println(list);
        return "list";
    }*/
    /*public String getEmps(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        //引入pageHelper
        PageHelper.startPage(pn,5);//传入页码和每页显示的数量
        List<Employee> emps = employeeService.getAll();
        //使用pageInfo封装查询后的结果,只需要将pageInfo交给页面就可以了
        //封装了详细的分页信息
        PageInfo page = new PageInfo(emps, 5);//5导航页码数
        model.addAttribute("pageInfo",page);
        return "list";
    }*/
//    此注解可以自动的把返回的数据转换成字符串
    @ResponseBody
    public Msg getEmpsWithJson(@RequestParam(value = "pn",defaultValue = "1")Integer pn){
//        引入pageHelper
//        传入页码和每页显示的数量
        PageHelper.startPage(pn,5);
        List<Employee> emps = employeeService.getAll();
//        使用pageInfo封装查询后的接口,只需要将pageInfo交给页面就可以了
        PageInfo page = new PageInfo(emps, 5);
        return Msg.success().add("pageInfo",page);
    }

    /*
    * 新增员工
    * */
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    @ResponseBody
    public Msg saveEmp(@Valid Employee employee, BindingResult result){
        if (result.hasErrors()){
            HashMap<String, Object> map = new HashMap<>();
            //校验失败,返回失败.
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError fieldError:
                 errors) {
                System.out.println("错误的字段名"+fieldError.getField());
                System.out.println("错误的信息:"+fieldError.getDefaultMessage());
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            return Msg.fail().add("errorFields",map);
        }else {
            employeeService.saveEmp(employee);
            return Msg.success();
        }
    }

    /*
    * 校验用户名
    * */
    @RequestMapping("/checkuser")
    @ResponseBody
    public Msg checkUser(@RequestParam("empName")String empName){
        System.out.println("check user");
        System.out.println("empName" + empName);
        boolean b = employeeService.checkUser(empName);
        System.out.println("b: "+b);
        if (b){
            return Msg.success();
        }else{
            return Msg.fail();
        }
    }

    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmp(@PathVariable("id") Integer id){
        Employee emp = employeeService.getEmp(id);
        return Msg.success().add("emp",emp);
    }


}
